<template>
	<view class="uni-common-pb uni-padding-wrap uni-common-mt">

		<view class="uni-form-item uni-row">
			<view class="title">当前制程</view>
			<view class="title">{{step_name}}&nbsp;&nbsp;{{step_code}}</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">工单编号</view>
			<view class="title">{{workordernumber}}</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">产品品号</view>
			<view class="title">{{productname}}</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">全长</view>
			<view class="title">{{fulllength}}</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">规格名</view>
			<view class="title">{{specname}}</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">发料记录</view>
			<view class="title">{{issuerecord}}</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">备注</view>
			<view class="title">{{info}}</view>
		</view>
		<view class="uni-form-item uni-row">
			<view class="title">研磨机编号</view>
			<view class="title">{{ymmachine}}</view>
		</view>
		<view class="uni-form-item uni-row">
			<view class="title">工序名统计</view>
			<view class="title">{{stepnames}}</view>
		</view>
		<view class="uni-form-item uni-row">
			<view class="title">实际完成时间</view>
			<view class="title">{{endtime}}</view>
		</view>
		<view class="line-item"></view>

		<form @submit="formSubmit">
			<view class="uni-form-item uni-row">
				<view class="title">作业员选择</view>
				<view class="uni-list-cell-db">
					<picker @change="bindPickerChange" :value="index" :range="operatorlist">
						<view class="uni-input">{{operatorlist[index]}}</view>
					</picker>
				</view>
			</view>

			<view class="uni-form-item uni-row">
				<view class="title">工时（小时）</view>
				<input class="uni-input" name="step_time" v-model="step_time" type="number" placeholder="请输入工时" />
			</view>

			<view class="uni-form-item uni-row">
				<view class="title">良品数（件）</view>
				<input class="uni-input" name="step_num_ok" v-model="step_num_ok" type="number" placeholder="请输入良品数" />
			</view>

			<view class="uni-form-item uni-row">
				<view class="title">不良品数（件）</view>
				<input class="uni-input" name="step_num_ng" v-model="step_num_ng" type="number" placeholder="请输入不良品数" />
			</view>
			<view class="uni-form-item uni-row">
				<view class="title">原料不良数（件）</view>
				<input class="uni-input" name="step_num_pm" v-model="step_num_pm" type="number" placeholder="请输入原料不良数" />
			</view>
			<view class="uni-form-item uni-row">
				<view class="title">报废断针数（件）</view>
				<input class="uni-input" name="step_num_scrap_a" v-model="step_num_scrap_a" type="number" placeholder="请输入报废断针数" />
			</view>

			<view class="uni-form-item uni-row">
				<view class="title">其他报废品数（件）</view>
				<input class="uni-input" name="step_num_scrap_b" v-model="step_num_scrap_b" type="number" placeholder="请输入其他报废品数" />
			</view>

			<view class="uni-form-item uni-row">
				<view class="title">报废原因</view>
				<textarea class="uni-textarea" name="scrap_content" v-model="scrap_content" placeholder="请输入报废原因"></textarea>
			</view>

			<view class="uni-form-item uni-row">
				<view class="title">备注</view>
				<textarea class="uni-textarea" name="content" v-model="content" placeholder="请输入备注"></textarea>
			</view>

			<view v-if="step_code==='S1'||step_code==='S1-5'||step_code==='S2' ">
				<view class="uni-form-item uni-row">
					<view class="title">清洗时间（小时）</view>
					<input class="uni-input" name="qingxitime" v-model="qingxitime" type="number" placeholder="请输入清洗时间" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">烘干时间（小时）</view>
					<input class="uni-input" name="honggantime" v-model="honggantime" type="number" placeholder="请输入烘干时间" />
				</view>
			</view>

			<view class="line-item"></view>

			<view v-if="step_code==='M' ">

				<view class="title">精磨检查</view>
				<view class="uni-form-item uni-row">
					<view class="title">直大数（件）</view>
					<input class="uni-input" name="m_num_ng1" v-model="m_num_ng1" type="number" placeholder="请输入直大数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">直小数（件）</view>
					<input class="uni-input" name="m_num_ng2" v-model="m_num_ng2" type="number" placeholder="请输入直小数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">锥大数（件）</view>
					<input class="uni-input" name="m_num_ng3" v-model="m_num_ng3" type="number" placeholder="请输入锥大数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">锥小数（件）</view>
					<input class="uni-input" name="m_num_ng4" v-model="m_num_ng4" type="number" placeholder="请输入锥小数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">真圆数（件）</view>
					<input class="uni-input" name="m_num_ng5" v-model="m_num_ng5" type="number" placeholder="请输入真圆数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">柄大/小数（件）</view>
					<input class="uni-input" name="m_num_ng6" v-model="m_num_ng6" type="number" placeholder="请输入柄大/小数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">刮伤数（件）</view>
					<input class="uni-input" name="m_num_ng7" v-model="m_num_ng7" type="number" placeholder="请输入刮伤数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">原料不良数（件）</view>
					<input class="uni-input" name="m_num_ng8" v-model="m_num_ng8" type="number" placeholder="请输入原料不良数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">偏摆数（件）</view>
					<input class="uni-input" name="m_num_ng9" v-model="m_num_ng9" type="number" placeholder="请输入偏摆数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">其它数（件）</view>
					<input class="uni-input" name="m_num_ng10" v-model="m_num_ng10" type="number" placeholder="请输入其它数" />
				</view>
			</view>

			<view v-if="step_code==='SJ-5' ">
				<view class="title">生五焊接</view>
				<view class="uni-form-item uni-row">
					<view class="title">歪头数（件）</view>
					<input class="uni-input" name="sj5_num_ng1" v-model="sj5_num_ng1" type="number" placeholder="请输入歪头数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">焊接颠数（件）</view>
					<input class="uni-input" name="sj5_num_ng2" v-model="sj5_num_ng2" type="number" placeholder="请输入焊接颠数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">焊缝不良数（件）</view>
					<input class="uni-input" name="sj5_num_ng3" v-model="sj5_num_ng3" type="number" placeholder="请输入焊缝不良数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">全长不良数（件）</view>
					<input class="uni-input" name="sj5_num_ng4" v-model="sj5_num_ng4" type="number" placeholder="请输入全长不良数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">外观不良数（件）</view>
					<input class="uni-input" name="sj5_num_ng5" v-model="sj5_num_ng5" type="number" placeholder="请输入外观不良数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">断针数（件）</view>
					<input class="uni-input" name="sj5_num_ng6" v-model="sj5_num_ng6" type="number" placeholder="请输入断针数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">混料数（件）</view>
					<input class="uni-input" name="sj5_num_ng7" v-model="sj5_num_ng7" type="number" placeholder="请输入混料数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">原料不良数（件）</view>
					<input class="uni-input" name="sj5_num_ng8" v-model="sj5_num_ng8" type="number" placeholder="请输入原料不良数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">偏摆数（件）</view>
					<input class="uni-input" name="sj5_num_ng9" v-model="sj5_num_ng9" type="number" placeholder="请输入偏摆数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">其他数（件）</view>
					<input class="uni-input" name="sj5_num_ng10" v-model="sj5_num_ng10" type="number" placeholder="请输入其他数" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">焊片厂家数（件）</view>
					<input class="uni-input" name="sj5_num_ng11" v-model="sj5_num_ng11" type="number" placeholder="请输入焊片厂家数" />
				</view>
			</view>

			<view class="line-item"></view>

			<view class="uni-flex uni-row">
				<button type="primary" formType="submit" style="flex: 1 1 0%;">提交</button>
			</view>
		</form>



	</view>

</template>

<script>
	const duration = 1000

	export default {
		data() {
			return {
				subtitle: '一般贸易成品工单',
				step_code: 'Q1',
				step_name: '全检',
				workordernumber: '',
				productname: '',
				fulllength: '',
				specname: '',
				issuerecord: '',
				info: '',
				ymmachine: '',
				stepnames: '',
				endtime: '未完成',
				step_num_ok: '',
				step_num_ng: '1',
				step_num_pm: '',
				step_num_scrap_a: '1',
				step_num_scrap_b: '1',
				step_time: '1',
				step_operator: '',
				workorder_id: '',
				processtype_id: '',
				content: '', //制程状态备注， 其他需要提交的信息
				scrap_content: '',

				m_num_ng1: '',
				m_num_ng2: '',
				m_num_ng3: '',
				m_num_ng4: '',
				m_num_ng5: '',
				m_num_ng6: '',
				m_num_ng7: '',
				m_num_ng8: '',
				m_num_ng9: '',
				m_num_ng10: '',
				sj5_num_ng1: '',
				sj5_num_ng2: '',
				sj5_num_ng3: '',
				sj5_num_ng4: '',
				sj5_num_ng5: '',
				sj5_num_ng6: '',
				sj5_num_ng7: '',
				sj5_num_ng8: '',
				sj5_num_ng9: '',
				sj5_num_ng10: '',
				sj5_num_ng11: '',
				qingxitime: '',
				honggantime: '',

				res: '',
				res1: '',
				operatorlist: [],
				index: 1111,

			}

		},
		onLoad(e) {
			console.log(e);
			this.workordernumber = e.workordernumber;

		},
		onReady: function() {
			console.log("onReady-->");
			uni.getStorage({
				key: 'zcdh',
				success: (res) => {
					this.step_code = res.data;
				},
				fail: () => {

				},
				complete: () => {
					//获取该制程的所有操作员
					console.log("getoperatorlist-->" + this.step_code);
					uni.request({
						url: this.neiUrl + '/api/workorder/getoperatorlist',
						data: {
							"step_code": this.step_code
						},
						success: (res) => {
							console.log(res.data.data);
							for (var i = 0; i < res.data.data.length; i++) {
								this.operatorlist[i] = res.data.data[i].name;
							}
						},
						fail: (err) => {
							console.log(err);
							uni.showToast({
								title: '获取操作员失败！' + err,
								icon: 'none',
								mask: true,
								duration: 3000
							});
						},
						complete: () => {

						}
					});
				}
			});
			uni.getStorage({
				key: 'zcname',
				success: (res) => {
					this.step_name = res.data;
				},
				fail: () => {

				}
			});


			this.getinfo();

		},
		// /相应右上角的功能按钮/ 
		onNavigationBarButtonTap(e) {
			uni.reLaunch({
				url: '/pages/main/main'
			});
		},
		methods: {
			formSubmit: function(e) {
				console.log('form发生了submit事件，携带数据为：' + JSON.stringify(e.detail.value))

				this.step_operator = this.operatorlist[this.index];
				// #ifdef APP-PLUS
				var endtime = Date.parse(new Date()) / 1000;
				var content = "工单编号:" + this.workordernumber +
					"\n规格：" + this.specname +
					"\n品号：" + this.productname +
					"\n发料记录：" + this.issuerecord +
					"\n备注：" + this.info +
					"\n当前制程：" + this.step_name + ' ' + this.step_code +
					"\n全长：" + this.fulllength +
					"\n良品数：" + this.step_num_ok +
					"\n不良品数：" + this.step_num_ng +
					"\n原料不良数：" + this.step_num_pm +
					"\n报废断针数：" + this.step_num_scrap_a +
					"\n其他报废品数：" + this.step_num_scrap_b +
					"\n作业员：" + this.step_operator +
					"\n工时：" + this.step_time + " 小时" +
					"\n备注：" + this.content +
					"\n报废原因：" + this.scrap_content +
					"\n完成时间：" + formatUnixtimestamp(endtime);

				if (this.step_code == 'M') {
					content += "\n直大数：" + this.m_num_ng1 +
						"\n直小数：" + this.m_num_ng2 +
						"\n锥大数：" + this.m_num_ng3 +
						"\n锥小数：" + this.m_num_ng4 +
						"\n真圆数：" + this.m_num_ng5 +
						"\n柄大/小数：" + this.m_num_ng6 +
						"\n刮伤数：" + this.m_num_ng7 +
						"\n原料不良数：" + this.m_num_ng8 +
						"\n偏摆数：" + this.m_num_ng9 +
						"\n其它数：" + this.m_num_ng10;
				} else if (this.step_code == 'SJ-5') {
					content += "\n歪头数：" + this.sj5_num_ng1 +
						"\n焊接颠数：" + this.sj5_num_ng2 +
						"\n焊缝不良数：" + this.sj5_num_ng3 +
						"\n全长不良数：" + this.sj5_num_ng4 +
						"\n外观不良数：" + this.sj5_num_ng5 +
						"\n断针数：" + this.sj5_num_ng6 +
						"\n混料数：" + this.sj5_num_ng7 +
						"\n原料不良数：" + this.sj5_num_ng8 +
						"\n偏摆数：" + this.sj5_num_ng9 +
						"\n其他数：" + this.sj5_num_ng10 +
						"\n焊片厂家数：" + this.sj5_num_ng11;
				} else if (this.step_code == 'S1' || this.step_code == 'S1-5' || this.step_code == 'S2') {
					content += "\n清洗时间：" + this.qingxitime +
						"\n烘干时间：" + this.honggantime;
				}

				const dcRichAlert = uni.requireNativePlugin('DCloud-RichAlert');
				dcRichAlert.show({
					position: 'bottom',
					title: "请确认您输入的信息是否正确",
					titleColor: '#FF0000',
					content: content,
					contentAlign: 'left',
					buttons: [{
						title: '提交',
						titleColor: '#3F51B5'

					}, {
						title: '取消'
					}]
				}, result => {
					if (result.type == 'button' && result.index == 0) {
						this.updateprocesslog();
					}
				});
				// #endif

				// #ifndef APP-PLUS
				this.updateprocesslog();
				// #endif

			},
			updateprocesslog: function() {
				var endtime = Date.parse(new Date()) / 1000;
				console.log(this.content);
				const requestTask3 = uni.request({
					url: this.neiUrl + '/api/workorder/updateprocesslog',
					data: {
						"workorder_id": this.workorder_id,
						"step_code": this.step_code,
						"endtime": endtime,
						"starttime": Date.parse(new Date()) / 1000 - this.step_time * 3600,
						"step_num_ok": this.step_num_ok,
						"step_num_ng": this.step_num_ng,
						"step_num_pm": this.step_num_pm,
						"step_num_scrap_a": this.step_num_scrap_a,
						"step_num_scrap_b": this.step_num_scrap_b,
						"step_operator": this.step_operator,
						"content": this.content,
						"scrap_content": this.scrap_content,
						"m_num_ng1": this.m_num_ng1,
						"m_num_ng2": this.m_num_ng2,
						"m_num_ng3": this.m_num_ng3,
						"m_num_ng4": this.m_num_ng4,
						"m_num_ng5": this.m_num_ng5,
						"m_num_ng6": this.m_num_ng6,
						"m_num_ng7": this.m_num_ng7,
						"m_num_ng8": this.m_num_ng8,
						"m_num_ng9": this.m_num_ng9,
						"m_num_ng10": this.m_num_ng10,
						"sj5_num_ng1": this.sj5_num_ng1,
						"sj5_num_ng2": this.sj5_num_ng2,
						"sj5_num_ng3": this.sj5_num_ng3,
						"sj5_num_ng4": this.sj5_num_ng4,
						"sj5_num_ng5": this.sj5_num_ng5,
						"sj5_num_ng6": this.sj5_num_ng6,
						"sj5_num_ng7": this.sj5_num_ng7,
						"sj5_num_ng8": this.sj5_num_ng8,
						"sj5_num_ng9": this.sj5_num_ng9,
						"sj5_num_ng10": this.sj5_num_ng10,
						"sj5_num_ng11": this.sj5_num_ng11,
						"qingxitime": this.qingxitime,
						"honggantime": this.honggantime,
					},
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: function(res) {
						console.log(JSON.stringify(res.data));
						uni.showToast({
							title: '制程状态已经提交！',
							icon: 'success',
							mask: true,
							duration: duration
						});
						uni.navigateBack();
					}
				});
			},
			getinfo: function() {
				console.log('getinfo ' + this.workordernumber);
				if (this.workordernumber.length < 1) {
					return;
				}

				uni.request({
					url: this.neiUrl + '/api/workorder/getinfo?',
					data: {
						"workordernumber": this.workordernumber
					},
					success: (res) => {
						console.log('request success', res)
						this.subtitle = res.data.data.subtitle;
						this.workordernumber = res.data.data.number;
						this.productname = res.data.data.productname;
						this.fulllength = res.data.data.fulllength;
						this.specname = res.data.data.specname;
						this.issuerecord = res.data.data.issuerecord;
						this.info = res.data.data.info;
						this.ymmachine = res.data.data.ymmachine;
						this.stepnames = res.data.data.stepnames;
						this.workorder_id = res.data.data.id;
						this.processtype_id = res.data.data.processtype_id;

						let title = this.subtitle;
						uni.setNavigationBarTitle({
							title: title,
							success: () => {
								console.log('setNavigationBarTitle success')
							},
							fail: (err) => {
								console.log('setNavigationBarTitle fail, err is', err)
							}
						})
						this.getprocesslog();
					},
					fail: (err) => {
						console.log('request fail', err);
						uni.showModal({
							content: err.errMsg,
							showCancel: false
						});
					},
					complete: () => {

					}
				});
			},
			getprocesslog: function() {
				uni.request({
					url: this.neiUrl + '/api/workorder/getprocesslog?',
					data: {
						"workorder_id": this.workorder_id,
						"step_code": this.step_code
					},
					success: (res) => {
						console.log('request success2', res)
						uni.showToast({
							title: '获取工单成功',
							icon: 'success',
							mask: true,
							duration: duration
						});
						this.endtime = formatUnixtimestamp(res.data.data.endtime);
						this.step_num_ok = res.data.data.step_num_ok;
						this.step_num_ng = res.data.data.step_num_ng;
						this.step_num_pm = res.data.data.step_num_pm;
						this.step_num_scrap_a = res.data.data.step_num_scrap_a;
						this.step_num_scrap_b = res.data.data.step_num_scrap_b;
						this.step_time = Math.round((res.data.data.endtime - res.data.data.starttime) / 3600);
						this.step_operator = res.data.data.step_operator;
						this.content = res.data.data.content;
						this.scrap_content = res.data.data.scrap_content;

						this.m_num_ng1 = res.data.data.m_num_ng1;
						this.m_num_ng2 = res.data.data.m_num_ng2;
						this.m_num_ng3 = res.data.data.m_num_ng3;
						this.m_num_ng4 = res.data.data.m_num_ng4;
						this.m_num_ng5 = res.data.data.m_num_ng5;
						this.m_num_ng6 = res.data.data.m_num_ng6;
						this.m_num_ng7 = res.data.data.m_num_ng7;
						this.m_num_ng8 = res.data.data.m_num_ng8;
						this.m_num_ng9 = res.data.data.m_num_ng9;
						this.m_num_ng10 = res.data.data.m_num_ng10;
						this.sj5_num_ng1 = res.data.data.sj5_num_ng1;
						this.sj5_num_ng2 = res.data.data.sj5_num_ng2;
						this.sj5_num_ng3 = res.data.data.sj5_num_ng3;
						this.sj5_num_ng4 = res.data.data.sj5_num_ng4;
						this.sj5_num_ng5 = res.data.data.sj5_num_ng5;
						this.sj5_num_ng6 = res.data.data.sj5_num_ng6;
						this.sj5_num_ng7 = res.data.data.sj5_num_ng7;
						this.sj5_num_ng8 = res.data.data.sj5_num_ng8;
						this.sj5_num_ng9 = res.data.data.sj5_num_ng9;
						this.sj5_num_ng10 = res.data.data.sj5_num_ng10;
						this.sj5_num_ng11 = res.data.data.sj5_num_ng11;
						this.qingxitime = res.data.data.qingxitime;
						this.honggantime = res.data.data.honggantime;

						for (var i = 0; i < this.operatorlist.length; i++) {
							if (this.operatorlist[i] == this.step_operator) {
								this.index = i;
							}
						}
						console.log("DDD=" + this.index + '  ' + this.step_operator);
					},
					fail: (err) => {
						console.log('request fail2', err);
						uni.showModal({
							content: err.errMsg,
							showCancel: false
						});
					},
					complete: () => {

					}
				});
			},
			bindPickerChange: function(e) {
				console.log('picker发送选择改变，携带值为：' + e.target.value)
				this.index = e.target.value;
				this.step_operator = this.operatorlist[this.index];
			},
		}
	}

	function formatUnixtimestamp(unixtimestamp) {
		var unixtimestamp = new Date(unixtimestamp * 1000);
		var year = 1900 + unixtimestamp.getYear();
		var month = "0" + (unixtimestamp.getMonth() + 1);
		var date = "0" + unixtimestamp.getDate();
		var hour = "0" + unixtimestamp.getHours();
		var minute = "0" + unixtimestamp.getMinutes();
		var second = "0" + unixtimestamp.getSeconds();
		return year + "-" + month.substring(month.length - 2, month.length) + "-" + date.substring(date.length - 2, date.length) +
			" " + hour.substring(hour.length - 2, hour.length) + ":" +
			minute.substring(minute.length - 2, minute.length) + ":" +
			second.substring(second.length - 2, second.length);
	}
</script>

<style>
	.content {
		text-align: center;
		height: 100upx;
	}

	.title {
		font-size: 30upx;
		width: 265upx;
	}

	.line-item {
		height: 1upx;
		background: #999999;
		color: #FFF;
	}

	button {
		margin-top: 30upx;
		margin-bottom: 30upx;
	}
</style>
